package net.ix.api.bookmark;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import net.ix.api.IXApi;

import org.dom4j.Element;
import org.dom4j.DocumentException;

import android.util.Log;

public class Catalogs extends IXApi{
	private static String tag = "Catalogs";
	private int start = 0;
	private int offset = 10;
	private static String method = "bookmark.catalogs";
	
	public Catalogs() {
		super(method);
		connect();
	}
	public Catalogs(int s, int o) {
		super(method);
		start = s;
		offset = o;
		connect();
	}
	private void connect(){
		Map<String, String> p = new HashMap<String, String>();
		p.put("start", start+"");
		p.put("offset", offset+"");
		addRequest(p);
		try {
			getXml();
		} catch (DocumentException e) {
			e.printStackTrace();
		}
	}
	public List<Map<String, Object>> getList(){
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		Map<String, Object> map;
		List<?> xmlList = resXML.selectNodes("/rsp/lists/list");
		Iterator<?> it = xmlList.iterator();
		while(it.hasNext()){
			Element listItem = (Element)it.next();
			map = new HashMap<String, Object>();
			map.put("title", listItem.element("title").getText());
			Log.i(tag, listItem.element("title").getText());
			list.add(map);
		}
		return list;
	}
	public int getCount(){
		int count = Integer.parseInt(resXML.selectSingleNode("/rsp/count").getStringValue());
		return count;
	}
}